// 课程报名相关（本地持久化模拟）

const STORAGE_KEY = 'enrolledCourses'

function readEnrolledSet() {
  try {
    const raw = localStorage.getItem(STORAGE_KEY)
    if (!raw) return new Set()
    const arr = JSON.parse(raw)
    if (Array.isArray(arr)) return new Set(arr)
    return new Set()
  } catch (e) {
    return new Set()
  }
}

function writeEnrolledSet(set) {
  const arr = Array.from(set)
  localStorage.setItem(STORAGE_KEY, JSON.stringify(arr))
}

export const isCourseEnrolled = (courseId) => {
  const set = readEnrolledSet()
  return set.has(String(courseId))
}

export const enrollCourse = async (courseId) => {
  await new Promise((r) => setTimeout(r, 300))
  const set = readEnrolledSet()
  set.add(String(courseId))
  writeEnrolledSet(set)
  return { success: true, message: '报名成功' }
}


